package net.maku.qdd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import net.maku.qdd.common.utils.PageResult;
import net.maku.qdd.convert.GoodsConvert;
import net.maku.qdd.entity.GoodsEntity;
import net.maku.qdd.query.GoodsQuery;
import net.maku.qdd.vo.GoodsVO;
import net.maku.qdd.dao.GoodsDao;
import net.maku.qdd.service.GoodsService;
import com.fhs.trans.service.impl.TransService;
import net.maku.qdd.common.utils.ExcelUtils;
import net.maku.qdd.vo.GoodsExcelVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
@AllArgsConstructor
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
    private final TransService transService;

    @Override
    public PageResult<GoodsVO> page(GoodsQuery query) {
        IPage<GoodsEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(GoodsConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }


    /**
     * 获取分页对象
     */
    private IPage<GoodsEntity> getPage(GoodsQuery query) {
        return new Page<>(query.getCurrent(), query.getSize());
    }

    /**
     * 获取查询包装器
     */
    private LambdaQueryWrapper<GoodsEntity> getWrapper(GoodsQuery query){
        LambdaQueryWrapper<GoodsEntity> wrapper = Wrappers.lambdaQuery();
        // 这里可以添加具体的查询条件
        return wrapper;
    }


    @Override
    public GoodsVO get(Long id) {
        GoodsEntity entity = baseMapper.selectById(id);
        GoodsVO vo = GoodsConvert.INSTANCE.convert(entity);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(GoodsVO vo) {
        GoodsEntity entity = GoodsConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GoodsVO vo) {
        GoodsEntity entity = GoodsConvert.INSTANCE.convert(vo);

        updateById(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);

    }



    @Override
    public void export() {
        List<GoodsExcelVO> excelList = GoodsConvert.INSTANCE.convertExcelList(list());
        ExcelUtils.excelExport(GoodsExcelVO.class, "商品", null, excelList);
    }

}